<template>
  <div v-loading="loading" class="app-container">
    <div class="filter-container">

      <!-- 头部导航搜索添加等 -->

      <el-input v-model="listQuery.title" placeholder="输入关键词" style="width: 200px;" class="filter-item"
        @input="handleFilter()" @keyup.enter.native="handleFilter" />

      <el-select v-model="listQuery.sort" style="width: 140px; margin-left: 10px;" class="filter-item"
        @change="handleFilter">
        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>

      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
        @click="handleCreate">
        新增
      </el-button>

    </div>

    <!-- 列表主体 -->
    <el-table :data="tableData" border fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
      <!--  -->
      <el-table-column label="ID" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人姓名" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人手机号" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <div v-if="row.is_lizhi==0">在职</div>
          <div v-if="row.is_lizhi!=0">离职</div>
        </template>
      </el-table-column>
      <el-table-column label="录入时间" min-width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="250" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">


          <el-button type="primary" size="mini"
            @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row,$index,row.id)">
            删除
          </el-button>
          <el-button v-if="row.is_lizhi==0" size="mini" type="info"
            @click="handleType(row,$index,row.id,1)">
            离职
          </el-button>
          <el-button v-if="row.is_lizhi==1" size="mini" type="warning"
            @click="handleType(row,$index,row.id,0)">
            入职
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total>0" :page-sizes="pagesizes" :total="total" :page.sync="listQuery.page"
      :limit.sync="listQuery.limit" @pagination="list" />

    <!-- 添加修改 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="top" label-width="70px"
        style="width: 400px; margin-left:50px;">

        <el-form-item label="负责人姓名" prop="name">
          <el-input v-model="temp.name" placeholder="负责人姓名" />
        </el-form-item>
        <el-form-item label="负责人手机号" prop="name">
          <el-input v-model="temp.phone" placeholder="负责人手机号" />
        </el-form-item>

        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">
            取消
          </el-button>
          <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
            确认
          </el-button>
        </div>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
  // 引入api
  import api from '@/api/pingtai/pzdata/people.js'

  // 引入分页
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination

  export default {
    components: { Pagination },
    data() {
      return {
        // 分页
        total: 0,
        pagesizes: [10, 30, 60, 100],
        // 列表数据
        tableData: [],
        // 权限列表
        roleData: null,
        // 权限列表筛选用
        roleData_: [
          {
            id: 0,
            name: '全部权限'
          }
        ],
        // 根据权限筛选
        rolemodel: 0,
        // 提交数据
        temp: {

        },
        // 密码
        password: '',
        // 判定加载
        loading: false,
        // 判定where
        listQuery: {
          page: 1,
          limit: 10,
          title: null,
          sort: 'asc'
        },
        // 判定显示
        dialogFormVisible: false,
        // 判定修改or添加
        dialogStatus: '',
        // 判定修改id
        up_id: 0,
        // 列表匹配数据群
        textMap: {
          update: '修改',
          create: '添加'
        },
        affix2: [
          {
            value: 0,
            label: '保留'
          },
          {
            value: 1,
            label: '不保留'
          }
        ],
        show: [
          {
            value: 0,
            label: '展示'
          },
          {
            value: 1,
            label: '不展示'
          }
        ],
        sortOptions: [
          { label: 'ID 升序', key: 'asc' },
          { label: 'ID 降序', key: 'desc' }
        ]
      }
    },
    // 进入页面加载数据
    created() {
      this.list()
    },
    methods: {
      go_info(id){

         this.$router.push({path:'/vote/gift_info',query:{id:id}});
      },
      // 冻结
      handleType(row, index, id, type) {
        var than = this
        var arr = ['解除冻结账号可正常登陆, 是否继续?', '冻结账号将无法正常登陆, 是否继续?']
        this.$confirm(arr[type], '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          api.frost({
            id: id,
            type: type
          }).then(() => {
            this.loading = false
            // this.list.unshift(this.temp)
            than.list()
            than.dialogFormVisible = false
            arr = ['禁用成功', '启用成功']
            than.$notify({
              title: '提示',
              message: arr[type],
              type: 'success',
              duration: 2000
            })
          })
        }).catch(() => {
          than.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      // 删除
      handleDelete(row, index, id) {
        var than = this
        this.$confirm('该操作将永久删除该账号, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          api.del({

            id: id
          }).then(() => {
            this.loading = false
            // this.list.unshift(this.temp)
            than.list()
            than.dialogFormVisible = false
            than.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
        }).catch(() => {
          than.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 修改
      updateData() {
        this.loading = true
        var than = this
        console.log(this.temp)
        this.temp.password = this.password
        api.update({

          id: this.up_id,
          temp: this.temp
        }).then(res => {
          this.list()
          this.dialogFormVisible = false
          // router.addRoutes(res)
        }).catch((res) => {
          console.log('22222')
          than.loading = false
        })
      },
      // 添加
      createData() {
        this.loading = true
        var than = this
        console.log(this.temp)
        this.temp.password = this.password
        api.save({

          temp: this.temp
        }).then(res => {
          this.list()
          this.dialogFormVisible = false
          // router.addRoutes(res)
        }).catch((res) => {
          console.log('22222')
          than.loading = false
        })
      },
      // 还原提交数据
      resetTemp() {
        this.password = ''
        for (var key in this.temp) {
          delete this.temp[key]
        }
      },
      // 添加前钩子
      handleCreate(pid) {
        this.resetTemp(pid)

        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      // 修改前钩子
      handleUpdate(row) {
        this.up_id = row.id
        this.password = ''
        this.temp = Object.assign({}, row) // copy obj
        console.log(this.temp)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      // 获取列表
      list(title) {
        this.loading = true
        var than = this
        api.list({

          role_id: this.rolemodel,
          listQuery: this.listQuery
        }).then(res => {
          than.tableData = res.data.list
          than.total = res.data.count

          than.loading = false
          // router.addRoutes(res)
        }).catch((res) => {
          console.log('22222')
          than.loading = false
        })
      },
      // 排序
      sortChange(data) {
        const { prop, order } = data
        if (prop === 'id') {
          this.sortByID(order)
        }
      },
      sortByID(order) {
        if (order === 'ascending') {
          this.listQuery.sort = 'asc'
        } else {
          this.listQuery.sort = 'desc'
        }
        this.handleFilter()
      },
      // 还原数据
      handleFilter() {
        this.listQuery.page = 1
        this.list()
      }
    }
  }
</script>
